//
//  SureOrderVC.m
//  YaoHeAPP
//
//  Created by xyh on 16/3/18.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "SureOrderVC.h"
#import "Masonry.h"

//#import "gouwucartModel.h"
#import "TongYong.h"

#import "TPKeyboardAvoidingTableView.h"

#import "Define.h"

#import "Controller.h"
#import "huopinVC.h"

#import "youhuiquanVC.h"

#import "moneybuyCV.h"

#import "NSString+formatWithFloat.h"

#import "JSONKit.h"
#define screenWidth self.view.frame.size.width
#define screenHeight self.view.frame.size.height
#define bgVCcolor [UIColor colorWithRed:235.f/255.f green:235.f/255.f blue:244.f/255.f alpha:1]
#define kOrdeRequirements @"请填写您对订单的特殊要求"

@interface SureOrderVC ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate>
{
    NSString * toBeString;//改变的字
    NSInteger weighttext;//编辑后的高度
    
    UITextView *liuyanText;
    
    UIView *vwimgtext;
    
    NSMutableArray *MoneytextArray;//费用
    
    UILabel *keyongNum;//可用数
    
    UILabel *rightlabel1;//未使用
    
    NSString *wukeyong;//可用数量
    NSString *weishiyong;//未使用（使用的东西）
    
    NSString *zongmoney;//总金额
    NSString *chukufei;//出库费
    NSString *duanbofei;//短驳费
    NSString *youhuiMoney;//优惠金额
    
    UILabel *shifuTextBootm;
    
    UILabel *shifuText;
    
    NSString *youhuiCode;//优惠券code
    
    CGFloat k_keyboard_offsetY;
    CGFloat k_keyboardHeight;
    
    NSDecimalNumber *shifuMoneyDM;//实付金额
    
}

@property(nonatomic,strong) UITableView *tableview;  //定义大表格

@end

@implementation SureOrderVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"SureOrderVC"];
    
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"SureOrderVC"];
    
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //注册一个通知监听通知过来的操作
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(upview:) name:@"upyouhui" object:nil];
    
    self.navigationController.navigationBar.hidden=YES;
    
    self.view.backgroundColor=bgVCcolor;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(140);  make.right.offset(0);
    }];
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"确认订单";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(self.view.frame.size.width/2-70);
        make.width.offset(140);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.width.offset(24);
        make.height.offset(22);
        make.top.offset(31);
    }];
    
    //顶部数组
    
    //中间线长度
    NSInteger online=(screenWidth-(30*3)-(40*2))/2;
    NSInteger jianju=(screenWidth-(60*3)-(25*2))/2;
    NSInteger yuanWandH=30;
    NSArray *topArray=@[@"确认订单",@"支付订单",@"已结算"];
    for (int i=0; i<topArray.count; i++) {
        UILabel *yuan=[[UILabel alloc]init];
        yuan.frame=CGRectMake(40+(30+online)*i, 140-15-30-10-20, yuanWandH, yuanWandH);
        yuan.layer.masksToBounds=YES;
        yuan.text=[NSString stringWithFormat:@"%d",i+1];
        yuan.textAlignment=NSTextAlignmentCenter;
        yuan.textColor=[UIColor whiteColor];
        yuan.tag=10+i;
        yuan.layer.cornerRadius=15;
        
        [navcBar addSubview:yuan];
        
        
        UILabel *toplable=[[UILabel alloc]init];
        toplable.frame=CGRectMake(25+(60+jianju)*i, 140-15-30-10-20+30+5, 60, 30);
        toplable.text=topArray[i];
        toplable.textAlignment=NSTextAlignmentCenter;
        toplable.font=[UIFont systemFontOfSize:14];
        toplable.textColor=[UIColor whiteColor];
        toplable.tag=100+i;
        if (i==0) {
            
            yuan.backgroundColor=[UIColor colorWithRed:146.f/255.f green:198.f/255.f blue:35.f/255.f alpha:1];
            yuan.alpha=1;
            
            toplable.alpha=1;
        }else{
            yuan.backgroundColor=[UIColor colorWithRed:91.f/255.f green:140.f/255.f blue:152.f/255.f alpha:0.5];
            yuan.alpha=0.5;
            
            toplable.alpha=0.5;
        }
        
        [navcBar addSubview:toplable];
        
        
    }
    
     for (int j=0; j<topArray.count-1; j++) {
     
         UIImageView *onlineimg=[[UIImageView alloc]init];
         onlineimg.backgroundColor=[UIColor colorWithRed:91.f/255.f green:140.f/255.f blue:152.f/255.f alpha:0.5];
         onlineimg.frame=CGRectMake(40+yuanWandH*(j+1)+online*j, 140-15-30-10-20+15, online, 1);
         [navcBar addSubview:onlineimg];

     }
    MoneytextArray=[[NSMutableArray alloc]init];
    weishiyong=@"未使用";
    zongmoney=@"￥0.00";
    chukufei=@"￥0.00";
    duanbofei=@"￥0.00";
    youhuiMoney=@"￥0.00";
    youhuiCode=@"";
    shifuMoneyDM=[[NSDecimalNumber alloc]initWithString:@"0"];
    
    [MoneytextArray addObject:zongmoney];
    [MoneytextArray addObject:chukufei];
    [MoneytextArray addObject:duanbofei];
    [MoneytextArray addObject:youhuiMoney];
    
    
    //获取相关信息
    [self getothermoney];
    
    
    weighttext=40;
    toBeString = kOrdeRequirements;
    //创建表格
    _tableview=[[UITableView alloc]initWithFrame:CGRectMake(0,145, self.view.frame.size.width, self.view.frame.size.height-140-50) style:UITableViewStylePlain];
    _tableview.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
    _tableview.delegate=self;
    _tableview.dataSource=self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
//    _tableview.separatorStyle=NO;
    [self.view addSubview:_tableview];
    
    
    //下部按钮
    UIView *bootmView=[[UIView alloc]init];
    bootmView.backgroundColor=[UIColor whiteColor];
    bootmView.frame=CGRectMake(0,screenHeight-50, screenWidth, 50);
    [self.view addSubview:bootmView];
    
    //结算
    UIButton *tijiaoBUt=[[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-80,1, 80, 49)];
    tijiaoBUt.backgroundColor=[UIColor colorWithRed:251.f/255.f green:87.f/255.f blue:60.f/255.f alpha:1];
    [tijiaoBUt setTitle:@"提交订单" forState:UIControlStateNormal];
    tijiaoBUt.titleLabel.font=[UIFont systemFontOfSize:14];
    [tijiaoBUt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [tijiaoBUt addTarget:self action:@selector(tijiaoBUtAction) forControlEvents:UIControlEventTouchUpInside];
    [bootmView addSubview:tijiaoBUt];

    //实付款
    shifuTextBootm=[[UILabel alloc]initWithFrame:CGRectMake(10, 20, screenWidth-80-10, 20)];
    //        shifuText.text=@"实付款：￥150150";
    shifuTextBootm.font=[UIFont systemFontOfSize:12];
    shifuTextBootm.textAlignment=NSTextAlignmentLeft;
    shifuTextBootm.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
    [bootmView addSubview:shifuTextBootm];
    //指定位置变色
    NSString *ss=[NSString stringWithFormat:@"实付款:￥%@元",[NSString notRounding:shifuMoneyDM afterPoint:2]];
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:ss];
    NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:@":"].location+1,noteStr.length-4 );
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];
    [shifuTextBootm setAttributedText:noteStr] ;

    UILabel *rightlabel=[[UILabel alloc]initWithFrame:CGRectMake(10,0, screenWidth-80-25, 20)];
    NSString *st;
    if ([self.isXianhuo isEqualToString:@"现货列表"]) {
        
        st=[NSString stringWithFormat:@"共1件商品"];
    }else{
    
       st=[NSString stringWithFormat:@"共%lu件商品",(unsigned long)self.OrderArray.count];
    }
    
    rightlabel.textAlignment=NSTextAlignmentLeft;
    rightlabel.font=[UIFont systemFontOfSize:11];
    rightlabel.text=st;
    rightlabel.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
    [bootmView addSubview:rightlabel];
    //分割线
    UIView *vwimg=[[UIView alloc]init];
    vwimg.backgroundColor=[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
    vwimg.frame=CGRectMake(0, 0, screenWidth, 1);
    [bootmView addSubview:vwimg];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark -----------------监控键盘--------------------------
- (void)keyboardWillShow:(NSNotification *)notification{
    NSDictionary *userInfo = [notification userInfo];
    CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
    k_keyboard_offsetY = keyboardRect.origin.y-8;
    k_keyboardHeight = keyboardRect.size.height;
    
    [UIView animateWithDuration:0.25 animations:^{
        
//        self.tableview.frame = CGRectMake(0, 145, screenWidth, screenHeight-145-k_keyboardHeight);
        self.view.frame = CGRectMake(0, -200, screenWidth, screenHeight);
        
    }];
    
}

- (void)keyboardWillHide:(NSNotification *)notification{
    
    [UIView animateWithDuration:0.25 animations:^{
//        self.tableview.frame = CGRectMake(0,145, screenWidth, screenHeight-145-50);
        self.view.frame = CGRectMake(0, 0, screenWidth, screenHeight);
        
    }];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
}



//相应观察者方法
-(void)upview:(NSNotification *)notification
{
    
    NSDictionary *dic = [notification object];//获取到传递的对象
    
    NSString *str=[NSString stringWithFormat:@"%@",dic[@"COUPON_RULES"]];
    NSString *strUrl = [str stringByReplacingOccurrencesOfString:@"&yen;" withString:@"￥"];
    weishiyong=strUrl;
    
    
    NSMutableDictionary *dicd=[[NSMutableDictionary alloc]init];
    [dicd setValue:@"1433739374266387" forKey:@"MEMBER_ID"];
    
    
    if ([self.isXianhuo isEqualToString:@"现货列表"]) {
        
        [dicd setObject:self.gouwuMDT.PRODUCT_ID forKey:@"PRODUCT_ID"];
        [dicd setObject:self.gouwuMDT.QUANTITY forKey:@"QUANTITY"];
        
    }else{

        NSMutableArray *productIDarray=[[NSMutableArray alloc]init];
        
        for (int i=0; i<self.OrderArray.count; i++) {
            
            gouwucartModel *goucarM=[[gouwucartModel alloc]initWith:self.OrderArray[i]];
            
            [productIDarray addObject:goucarM.PRODUCT_ID];
            
        }
        
        NSString *string = [productIDarray componentsJoinedByString:@","];
        
        [dicd setValue:string forKey:@"PRODUCT_IDS"];
    }

    youhuiCode=[NSString stringWithFormat:@"%@",dic[@"BATCH_DETAIL_CODE"]];
    
    [dicd setValue:youhuiCode forKey:@"COUPON_DETAIL_CODE"];
    [Controller requestWithURL:GetDiscountMoney jsondic:dicd finishBlock:^(ClassData* data) {
        
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
        {
            NSString *num=[NSString stringWithFormat:@"%@",data.data];
            
            NSDecimalNumber *decimalNumber1 = [[NSDecimalNumber alloc] initWithString:num];
            shifuMoneyDM=[shifuMoneyDM decimalNumberBySubtracting:decimalNumber1];

            youhuiMoney=[NSString stringWithFormat:@"-￥%@",[NSString notRounding:decimalNumber1 afterPoint:2]];
            MoneytextArray[3]=youhuiMoney;
            
             [self.tableview reloadData];
            
             [self setbootmValue];
            
        }else
        {
            ALERTSHOW(data.message);
            
            
        }
    }];

    
    
}



//更新底部数据
-(void)setbootmValue
{

    //指定位置变色
    NSString *ss=[NSString stringWithFormat:@"实付款:￥%@",[NSString notRounding:shifuMoneyDM afterPoint:2]];
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:ss];
    NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:@":"].location+1,noteStr.length-4 );
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];
    [shifuTextBootm setAttributedText:noteStr] ;


}


//提交订单
-(void)tijiaoBUtAction
{
    [MobClick event:@"confirmOrder"];
    
    NSString *st;
    if ([self.isXianhuo isEqualToString:@"现货列表"]) {
        
       st=[NSString stringWithFormat:@"0"];
    }else{
        
       st=[NSString stringWithFormat:@"1"];
    }
    
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    //是否来自购物车
    [dic setValue:st forKey:@"from_shopping_cart"];
    //实付金额
     NSString *stMon=[NSString stringWithFormat:@"%@",[NSString notRounding:shifuMoneyDM afterPoint:2]];
    [dic setValue:stMon forKey:@"pay_amount"];
    //优惠券code
    [dic setValue:youhuiCode forKey:@"coupon_code"];
    
    //开票类型
    [dic setValue:@"10" forKey:@"invoice_type"];
    
    //addr_id
    [dic setValue:@"" forKey:@"addr_id"];
    
    [dic setValue:@"" forKey:@"self_invoicing_suppliers"];
    
    [dic setValue:@"0" forKey:@"payment_type"];
    
    [dic setValue:@"0" forKey:@"buy_type"];
    
    [dic setValue:@"" forKey:@"rule_id"];
    
    [dic setValue:@"50" forKey:@"order_source"];
    
    
    NSMutableArray *productsArray=[[NSMutableArray alloc]init];
    
    NSMutableDictionary *prdic=[[NSMutableDictionary alloc]init];
    
    //判断是否是从现货传过来的
    
    if ([self.isXianhuo isEqualToString:@"现货列表"]) {
        
        [prdic setObject:self.gouwuMDT.PRODUCT_ID forKey:@"product_id"];
        [prdic setObject:self.gouwuMDT.QUANTITY forKey:@"quantity"];
        [prdic setObject:@"0" forKey:@"processing_status"];
        [prdic setObject:@"" forKey:@"processing_note"];
        [prdic setObject:@"" forKey:@"remark"];
        
        [productsArray addObject:prdic];
        
    }else{
        
        
        for (int i=0; i<self.OrderArray.count; i++) {
            
            NSMutableDictionary *prdic1=[[NSMutableDictionary alloc]init];
            
            gouwucartModel *goucarM=[[gouwucartModel alloc]initWith:self.OrderArray[i]];
            
            [prdic1 setObject:goucarM.PRODUCT_ID forKey:@"product_id"];
            [prdic1 setObject:goucarM.QUANTITY forKey:@"quantity"];
            [prdic1 setObject:@"0" forKey:@"processing_status"];
            [prdic1 setObject:@"" forKey:@"processing_note"];
            [prdic1 setObject:@"" forKey:@"remark"];

            [productsArray addObject:prdic1];
            
        }
        
        
        
    }
    
   
    NSString *proListjson=[productsArray JSONString];
    
    [dic setValue:proListjson forKey:@"products"];//需要传一个数组
    [Controller requestWithURL:SUREORDER jsondic:dic finishBlock:^(ClassData* data) {
        
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
        {
            NSMutableDictionary *datedic=[[NSMutableDictionary alloc]init];
            datedic=data.data;
            
            if (datedic.count==1) {
                //key1 不存在  说明有错误信息
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                                message:@"您购买的货品中部分“已售罄”或“库存不足"
                                                               delegate:self
                                                      cancelButtonTitle:@"返回"
                                                      otherButtonTitles:nil];
                
                //这个属性继承自UIView，当一个视图中有多个AlertView时，可以用这个属性来区分
                alert.tag = 5;
                //只读属性，看AlertView是否可见
                [alert show];

                
            }else{
//            "batch_id" = 1459839909039553;
//            "expired_time" = 1799820;
//            "intent_order_id" = 1459839909918522;
           
            //需要修改，只有确定订单后才进入（金额是否充足）
            
            moneybuyCV *moneyvc=[[moneybuyCV alloc]init];
            if ([self.isXianhuo isEqualToString:@"现货列表"]) {
                //单条支付
                moneyvc.pay_type=@"1";
                moneyvc.order_ids=self.gouwuMDT.PRODUCT_ID;
                
                
            }else{
                
                NSMutableArray *productIDarray=[[NSMutableArray alloc]init];
                
                for (int i=0; i<self.OrderArray.count; i++) {
                    
                    gouwucartModel *goucarM=[[gouwucartModel alloc]initWith:self.OrderArray[i]];
                    
                    [productIDarray addObject:goucarM.PRODUCT_ID];
                    
                }
                
                NSString *string = [productIDarray componentsJoinedByString:@","];
                
                //单条
                moneyvc.pay_type=@"1";
                
                moneyvc.order_ids=string;
                
            }
                
                
//            NSMutableArray *arrayintent_id=[[NSMutableArray alloc]init];
//            arrayintent_id=data.data[@"orders"];
//                
//            NSMutableArray *intent_IDarray=[[NSMutableArray alloc]init];
//            for (int j=0; j<arrayintent_id.count; j++) {
//                    
//                [intent_IDarray addObject:arrayintent_id[j][@"intent_order_id"]];
//                    
//                }
             
//            NSString *stringintent_id = [intent_IDarray componentsJoinedByString:@","];
//            moneyvc.intent_order_id=stringintent_id;
//            moneyvc.ordernum=arrayintent_id.count;
                
                NSString *str =[NSString stringWithFormat:@"%@",data.data[@"intent_order_id"]];
                moneyvc.intent_order_id=str;
                
//                if ([str containsString:@","]) {
//                    
//                    moneyvc.ordernum=2;
//                } else {
//                    moneyvc.ordernum=1;
//                }
                
                if ([str rangeOfString:@","].location != NSNotFound) {
                
                    moneyvc.ordernum=2;
                    
                }else{
                    
                    moneyvc.ordernum=1;
                
                }
                
                
               //获取当前时间值
                NSTimeInterval timest = [[NSDate date] timeIntervalSince1970];
                
                NSInteger timeintage=timest;
                
                NSString *sttime=data.data[@"expired_time"];
                
                 NSInteger extime=sttime.integerValue/1000;
                
                NSInteger timevely=timeintage+extime;
                
                
            moneyvc.odertime=[NSString stringWithFormat:@"%ld",(long)timevely];
            moneyvc.batch_id=data.data[@"batch_id"];
            [self.navigationController pushViewController:moneyvc animated:YES];
                
        }
          
        }else
        {
            ALERTSHOW(data.message);
            
        }
    }];


     
}

-(void)getothermoney
{
//判断是否是从现货传过来的
    
    NSString *string;
    NSMutableDictionary *di=[[NSMutableDictionary alloc]init];
    [di setValue:@"1433739374266387" forKey:@"MEMBER_ID"];
    
    if ([self.isXianhuo isEqualToString:@"现货列表"]) {
        [di setObject:self.gouwuMDT.PRODUCT_ID forKey:@"PRODUCT_ID"];
        
        [di setObject:self.gouwuMDT.QUANTITY forKey:@"QUANTITY"];
    }else{
    
    
        NSMutableArray *productIDarray=[[NSMutableArray alloc]init];
        
        for (int i=0; i<self.OrderArray.count; i++) {
            
            gouwucartModel *goucarM=[[gouwucartModel alloc]initWith:self.OrderArray[i]];
            
            [productIDarray addObject:goucarM.PRODUCT_ID];
            
        }
        
        string = [productIDarray componentsJoinedByString:@","];
        [di setValue:string forKey:@"PRODUCT_IDS"];
    }
    [Controller requestWithURL:GOSTOTHERMONEY jsondic:di finishBlock:^(ClassData* data) {
        
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
        {
            NSInteger youhui=[data.data[@"COUPON_MOUNT"] integerValue];
            if (youhui==0) {
                wukeyong=@"无可用";
            }else{
//                wukeyong=[NSString stringWithFormat:@"%ld张可用",(long)youhui];
                
                wukeyong=@"";
            }
            
            NSString *st=[NSString stringWithFormat:@"%@",data.data[@"TOTAL_MONEY"]];
            NSDecimalNumber *num = [[NSDecimalNumber alloc] initWithString:st];
            
            zongmoney=[NSString stringWithFormat:@"%@",[NSString notRounding:num afterPoint:2]];
            
            
            NSDecimalNumber *chuku = [[NSDecimalNumber alloc] initWithString:data.data[@"LIBRARY_MONEY"]];
            
            chukufei=[NSString stringWithFormat:@"￥%@",[NSString notRounding:chuku afterPoint:2]];
            
            NSDecimalNumber *duanbo = [[NSDecimalNumber alloc] initWithString:data.data[@"DRAYABLE_MONEY"]];
            duanbofei=[NSString stringWithFormat:@"￥%@",[NSString notRounding:duanbo afterPoint:2]];
            
            shifuMoneyDM=[num decimalNumberByAdding:chuku];
            shifuMoneyDM=[shifuMoneyDM decimalNumberByAdding:duanbo];
            
            NSString *zongmoneyst=[NSString stringWithFormat:@"￥%@",zongmoney];
            MoneytextArray[0]=zongmoneyst;
            MoneytextArray[1]=chukufei;
            MoneytextArray[2]=duanbofei;
            MoneytextArray[3]=youhuiMoney;
        
            [self.tableview reloadData];
            
            [self setbootmValue];
            
//            "COUPON_MOUNT" = 1;//优惠券可用数量
//            "DRAYABLE_MONEY" = "45.71";//短驳费
//            "LIBRARY_MONEY" = "59.99";//出库费
//            "TOTAL_MONEY" = "24569.322800";//订单总金额
            
            
        }else
        {
            ALERTSHOW(data.message);
            
            
        }
    }];

    
    

}


-(void)backLogin
{
    
    //初始化AlertView
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                    message:@"现货资源库存有限，出货较快。再考虑下？"
                                                   delegate:self
                                          cancelButtonTitle:@"去逛逛"
                                          otherButtonTitles:@"再考虑下",nil];
    
    //这个属性继承自UIView，当一个视图中有多个AlertView时，可以用这个属性来区分
    alert.tag = 0;
    [alert show];

    
}
//根据被点击按钮的索引处理点击事件
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==0) {
        
        if (buttonIndex==1) {
            
        }else if(buttonIndex==0){
            //取消
            [self.navigationController popViewControllerAnimated:YES];
            
        }
        
    }else if(alertView.tag==5){
    
        if(buttonIndex==0){
            //取消
            [self.navigationController popViewControllerAnimated:YES];
            
        }

    }
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([self.isXianhuo isEqualToString:@"现货列表"]) {
        
        return 5;
        
    }
    
    
    return self.OrderArray.count+4;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.isXianhuo isEqualToString:@"现货列表"]) {
        
        if(indexPath.row==2){
            
            return weighttext;
            
        }
        if (indexPath.row==4) {
            
            return 210;
            
        }

        
    }else{
    
    
    
    if(indexPath.row==self.OrderArray.count+1){
    
        return weighttext;
        
    }
    if (indexPath.row==self.OrderArray.count+3) {
        
        return 210;
        
    }
}
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //指定cellIdentifier为自定义的cell
//    static NSString *CellIdentifier = @"OrderListCell";
    
    
    NSString *identifier = [NSString stringWithFormat:@"TimeLineCell%ld%ld",(long)indexPath.section,(long)indexPath.row];
    
    //自定义cell类有图片
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    
    UILabel *youhui;
    UILabel *youhuiText;
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        
        
        if (indexPath.row==0) {
            
            UILabel *leftlabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 60, 20)];
            leftlabel.text=@"货品清单";
            leftlabel.font=[UIFont systemFontOfSize:12];
            leftlabel.textAlignment=NSTextAlignmentLeft;
            leftlabel.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
            [cell addSubview:leftlabel];
            
            UILabel *rightlabel=[[UILabel alloc]initWithFrame:CGRectMake(70, 10, screenWidth-80-25, 20)];
            
            NSString *st;
            if ([self.isXianhuo isEqualToString:@"现货列表"]) {
                
                st=[NSString stringWithFormat:@"共1件,查看详情"];
            }else{
            
                st=[NSString stringWithFormat:@"共%lu件,查看详情",(unsigned long)self.OrderArray.count];
            }
            
            rightlabel.textAlignment=NSTextAlignmentRight;
            rightlabel.font=[UIFont systemFontOfSize:12];
            rightlabel.text=st;
            rightlabel.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
            [cell addSubview:rightlabel];
            
            
            //分割线
            UIView *vwimg=[[UIView alloc]init];
            vwimg.backgroundColor=[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
            vwimg.frame=CGRectMake(10, 40-1, screenWidth-1, 1);
            [cell addSubview:vwimg];
            
            
        }
        
        
        
        if ([self.isXianhuo isEqualToString:@"现货列表"]) {
            
            if (indexPath.row==1) {
                cell.backgroundColor=[UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0  alpha:1];
                cell.accessoryType=UITableViewCellAccessoryNone;
                
                gouwucartModel *gouwuMD=self.gouwuMDT;
                
                //顶部字符串拼接
                NSString *toplable;
                if ([TongYong isBlankObjectAndStr:gouwuMD.SURFACE_CODE]) {
                    toplable=[NSString stringWithFormat:@"%@",gouwuMD.MATERIAL_CODE];
                }else{
                    toplable=[NSString stringWithFormat:@"%@/%@",gouwuMD.MATERIAL_CODE,gouwuMD.SURFACE_CODE];
                    
                }
                
                
                NSString *guige=gouwuMD.SPEC_CODE;
                
                //参厚
                NSString *canhou;
                if ([TongYong isBlankObjectAndStr:gouwuMD.REFERENCE_THICK ]) {
                    canhou=@"";
                }else{
                    canhou=[NSString stringWithFormat:@"(%@)",gouwuMD.REFERENCE_THICK];
                }
                
                //等级
                NSString *dengji;
                if ([TongYong isBlankObjectAndStr:gouwuMD.LEVEL_NAME ]) {
                    
                    dengji=@"";
                    
                }else{
                    
                    dengji=[NSString stringWithFormat:@"%@",gouwuMD.LEVEL_NAME];
                    
                }
                
                
                //形状
                NSString *xinzhuang;
                if ([TongYong isBlankObjectAndStr:gouwuMD.SHAPE ]) {
                    
                    xinzhuang=@"";
                    
                }else{
                    
                    xinzhuang=[NSString stringWithFormat:@"/%@",gouwuMD.SHAPE];
                    
                }
                
                //品名
                NSString *pingming;
                if ([TongYong isBlankObjectAndStr:gouwuMD.CATEGORY_NAME ]) {
                    
                    pingming=@"";
                    
                }else{
                    
                    pingming=[NSString stringWithFormat:@"/%@",gouwuMD.CATEGORY_NAME];
                    
                }
                
                //仓库
                NSString *ck;
                if ([TongYong isBlankObjectAndStr:gouwuMD.HOUSE_NAME ]) {
                    
                    ck=@"";
                    
                }else{
                    
                    ck=[NSString stringWithFormat:@"%@",gouwuMD.HOUSE_NAME];
                    
                }
                
                //单位类型
                NSInteger unitpric=gouwuMD.PRICE_UNIT.integerValue;
//                //计算重量
//                float weig;
//                NSString *weightName;
//                if (unitpric==10) {
//                    //元每吨
//                    
//                    weig =  gouwuMD.UnitWeight.doubleValue/1000 * gouwuMD.QUANTITY.integerValue;
//                    weightName=[NSString stringWithFormat:@"%.3f吨",weig];
//                    
//                }else if(unitpric==20||unitpric==40){
//                    //支或张
//                    //单位
////                     if([gouwuMD.PARENT_NAME isEqualToString:@"板"]||[gouwuMD.PARENT_NAME isEqualToString:@"板材"]){
////                      weightName=[NSString stringWithFormat:@"%ld张",gouwuMD.QUANTITY.integerValue];
////                        
////                    }else if([gouwuMD.PARENT_NAME isEqualToString:@"管"]||[gouwuMD.PARENT_NAME isEqualToString:@"管材"]){
////                      weightName=[NSString stringWithFormat:@"%ld支",gouwuMD.QUANTITY.integerValue];
////                        
////                    }
//                    
//                    
//                    weig =  gouwuMD.UnitWeight.doubleValue * gouwuMD.QUANTITY.integerValue;
//                    weightName=[NSString stringWithFormat:@"%.3f千克",weig];
//
//                    
//                    
//                }else{
//                    //千克
//                    weig =  gouwuMD.UnitWeight.doubleValue * gouwuMD.QUANTITY.integerValue;
//                    weightName=[NSString stringWithFormat:@"%.3f千克",weig];
//                }
                
                //并且计算出金额
                //计算金额
                //单位类型
                NSString *QUANTITY=[NSString stringWithFormat:@"%@",gouwuMD.QUANTITY];
                NSString *weight=[NSString stringWithFormat:@"%@",gouwuMD.USEABLE_WEIGHT];
               
                NSDecimalNumber *quantityDM = [[NSDecimalNumber alloc] initWithString:QUANTITY];//个数
                NSDecimalNumber *weightDM = [[NSDecimalNumber alloc] initWithString:weight];//单个重量
                
                NSDecimalNumber *zongweightDM=[[NSDecimalNumber alloc]initWithString:@"0"];
                
                NSString *weightName;
                if (unitpric==10) {
                    //元每吨
                    NSDecimalNumber *qian = [[NSDecimalNumber alloc] initWithString:@"1000"];
                    zongweightDM = [quantityDM decimalNumberByMultiplyingBy:weightDM];
                    zongweightDM=[zongweightDM decimalNumberByDividingBy:qian];
                    weightName=[NSString stringWithFormat:@"%@吨",[NSString notRounding:zongweightDM afterPoint:3]];
                } else if (unitpric==20||unitpric==40) {
                    //支或张
                    zongweightDM = [quantityDM decimalNumberByMultiplyingBy:weightDM];
                    weightName=[NSString stringWithFormat:@"%@千克",[NSString notRounding:zongweightDM afterPoint:3]];
                    
                } else {
                    //千克
                    zongweightDM = [quantityDM decimalNumberByMultiplyingBy:weightDM];
                    weightName=[NSString stringWithFormat:@"%@千克",[NSString notRounding:zongweightDM afterPoint:3]];
                    
                }
                
    
                NSString *bigTEXT;
                if ([gouwuMD.CATEGORY_NAME isEqualToString:@"板"]||[gouwuMD.CATEGORY_NAME isEqualToString:@"板材"]||[gouwuMD.CATEGORY_NAME isEqualToString:@"卷材"]||[gouwuMD.CATEGORY_NAME isEqualToString:@"卷"]||[gouwuMD.CATEGORY_NAME isEqualToString:@"带材"]||[gouwuMD.CATEGORY_NAME isEqualToString:@"带"]) {
                    //            材质 规格(厚度*宽度*长度） 参厚 等级 重量 仓库
                    
                    bigTEXT=[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@",toplable,guige,canhou,dengji,weightName,ck];
                    
                    
                }else{
                    
                    //       材质表面 规格 品类（如扁钢等） 形状（如方管、圆管等，客户没有上传就不展示）
                    
                    bigTEXT=[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@",toplable,guige,gouwuMD.CATEGORY_NAME,gouwuMD.SHAPE,weightName,ck];
                }
                
                
                UILabel *contTextlabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, screenWidth-10, 40)];
                contTextlabel.text=bigTEXT;
                contTextlabel.font=[UIFont systemFontOfSize:12];
                contTextlabel.textAlignment=NSTextAlignmentLeft;
//                contTextlabel.
                [contTextlabel setNumberOfLines:0];
                contTextlabel.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                [cell addSubview:contTextlabel];
                
                
                //分割线
                UIView *vwimg=[[UIView alloc]init];
                vwimg.backgroundColor=[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
                vwimg.frame=CGRectMake(10, 40-1, screenWidth-1, 1);
                [cell addSubview:vwimg];
                
                
                
            }

            
            
            
            
            if (indexPath.row==2) {
                
                UILabel *liuyan=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 60, 20)];
                liuyan.text=@"买家留言:";
                liuyan.font=[UIFont systemFontOfSize:12];
                liuyan.textAlignment=NSTextAlignmentLeft;
                liuyan.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                [cell addSubview:liuyan];
                
                //留言填写
                liuyanText = [[UITextView alloc]initWithFrame:CGRectMake(70, 5, screenWidth-80, weighttext-10)];
                liuyanText.font = [UIFont systemFontOfSize:12];
                liuyanText.textAlignment = NSTextAlignmentLeft;
                liuyanText.text = toBeString;
                liuyanText.delegate = self;
                liuyanText.textColor = RGBCOLOR(153, 153, 153);
                [cell addSubview:liuyanText];
                
                
                //分割线
                vwimgtext=[[UIView alloc]init];
                vwimgtext.backgroundColor=[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
                vwimgtext.frame=CGRectMake(10, weighttext-1, screenWidth-1, 1);
                [cell addSubview:vwimgtext];
                
                
                
            }
            
            
            if (indexPath.row==3) {
                
                UILabel *youhui=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 60, 20)];
                youhui.text=@"优惠券";
                youhui.font=[UIFont systemFontOfSize:12];
                youhui.textAlignment=NSTextAlignmentLeft;
                youhui.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                [cell addSubview:youhui];
                
                
//                keyongNum=[[UILabel alloc]initWithFrame:CGRectMake(70, 10, 70, 20)];
//                keyongNum.text=wukeyong;
//                keyongNum.font=[UIFont systemFontOfSize:12];
//                keyongNum.textAlignment=NSTextAlignmentLeft;
//                keyongNum.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
//                [cell addSubview:keyongNum];
                
                UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(70, 10, 45, 20)];
                imageview.image=[UIImage imageNamed:@"wukeyongimg"];
                
                imageview.tag=200+indexPath.row;
                [cell addSubview:imageview];
                
                if ([wukeyong isEqualToString:@"无可用"]) {
                    imageview.hidden=NO;
                }else{
                    imageview.hidden=YES;
                }
                
                
                
                rightlabel1=[[UILabel alloc]initWithFrame:CGRectMake(60, 10, screenWidth-60-30, 20)];
                //        NSString *st=[NSString stringWithFormat:@"共%d件,查看详情",self.OrderArray.count];
                rightlabel1.textAlignment=NSTextAlignmentRight;
                rightlabel1.font=[UIFont systemFontOfSize:12];
                rightlabel1.text=weishiyong;
                rightlabel1.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                [cell addSubview:rightlabel1];
                
                //分割线
                UIView *vwimg=[[UIView alloc]init];
                vwimg.backgroundColor=[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
                vwimg.frame=CGRectMake(10, 40-1, screenWidth-1, 1);
                [cell addSubview:vwimg];
                
                
            }
            if (indexPath.row==4) {
                
                
                
                cell.backgroundColor=[UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
                
                UIView *fatherView=[[UIView alloc]initWithFrame:CGRectMake(0, 10, screenWidth, 200)];
                fatherView.backgroundColor=[UIColor whiteColor];
                [cell addSubview:fatherView];
                
                
                NSArray *array=@[@"货品总金额",@"出库费",@"短驳费",@"优惠金额"];
                for (int i=0; i<array.count; i++) {
                    
                    youhui=[[UILabel alloc]initWithFrame:CGRectMake(10, 10+(20+10+10)*i, 60, 20)];
                    youhui.text=array[i];
                    youhui.font=[UIFont systemFontOfSize:12];
                    youhui.textAlignment=NSTextAlignmentLeft;
                    youhui.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                    [fatherView addSubview:youhui];
                    
                    
                    youhuiText=[[UILabel alloc]initWithFrame:CGRectMake(80, 10+(20+10+10)*i, screenWidth-80-10, 20)];
                    youhuiText.text=MoneytextArray[i];
                    youhuiText.tag=1000+i;
                    youhuiText.font=[UIFont systemFontOfSize:12];
                    youhuiText.textAlignment=NSTextAlignmentRight;
                    youhuiText.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                    [fatherView addSubview:youhuiText];
                    
                    
                }
                
                //实付款
                shifuText=[[UILabel alloc]initWithFrame:CGRectMake(80, 10+160, screenWidth-80-10, 20)];
                //        shifuText.text=@"实付款：￥150150";
                shifuText.font=[UIFont systemFontOfSize:12];
                shifuText.textAlignment=NSTextAlignmentRight;
                shifuText.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                [fatherView addSubview:shifuText];
                
            }

            
            
            
            
            
        }else{
            
            
            for (int i=0; i<self.OrderArray.count; i++) {
                
                if (indexPath.row==i+1) {
                    cell.backgroundColor=[UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0  alpha:1];
                    cell.accessoryType=UITableViewCellAccessoryNone;
                    
                    gouwucartModel *gouwuMD=[[gouwucartModel alloc]initWith:self.OrderArray[i]];
                    
                    //顶部字符串拼接
                    NSString *toplable;
                    if ([TongYong isBlankObjectAndStr:gouwuMD.SURFACE_CODE]) {
                        toplable=[NSString stringWithFormat:@"%@",gouwuMD.MATERIAL_CODE];
                    }else{
                        toplable=[NSString stringWithFormat:@"%@/%@",gouwuMD.MATERIAL_CODE,gouwuMD.SURFACE_CODE];
                        
                    }
                    
                    
                    NSString *guige=gouwuMD.SPEC_CODE;
                    
                    //参厚
                    NSString *canhou;
                    if ([TongYong isBlankObjectAndStr:gouwuMD.REFERENCE_THICK ]) {
                        canhou=@"";
                    }else{
                        canhou=[NSString stringWithFormat:@"(%@)",gouwuMD.REFERENCE_THICK];
                    }
                    
                    //等级
                    NSString *dengji;
                    if ([TongYong isBlankObjectAndStr:gouwuMD.LEVEL_NAME ]) {
                        
                        dengji=@"";
                        
                    }else{
                        
                        dengji=[NSString stringWithFormat:@"%@",gouwuMD.LEVEL_NAME];
                        
                    }
                    
                    
                    //形状
                    NSString *xinzhuang;
                    if ([TongYong isBlankObjectAndStr:gouwuMD.SHAPE ]) {
                        
                        xinzhuang=@"";
                        
                    }else{
                        
                        xinzhuang=[NSString stringWithFormat:@"/%@",gouwuMD.SHAPE];
                        
                    }
                    
                    //品名
                    NSString *pingming;
                    if ([TongYong isBlankObjectAndStr:gouwuMD.CATEGORY_NAME ]) {
                        
                        pingming=@"";
                        
                    }else{
                        
                        pingming=[NSString stringWithFormat:@"/%@",gouwuMD.CATEGORY_NAME];
                        
                    }
                    
                    //仓库
                    NSString *ck;
                    if ([TongYong isBlankObjectAndStr:gouwuMD.HOUSE_NAME ]) {
                        
                        ck=@"";
                        
                    }else{
                        
                        ck=[NSString stringWithFormat:@"%@",gouwuMD.HOUSE_NAME];
                        
                    }
                    
                    //并且计算出金额
                    //计算金额
                    //单位类型
                    NSInteger unitpric=gouwuMD.PRICE_UNIT.integerValue;
                    NSString *QUANTITY=[NSString stringWithFormat:@"%@",gouwuMD.QUANTITY];
                    NSString *weight=[NSString stringWithFormat:@"%@",gouwuMD.USEABLE_WEIGHT];
                    
                    NSDecimalNumber *quantityDM = [[NSDecimalNumber alloc] initWithString:QUANTITY];//个数
                    NSDecimalNumber *weightDM = [[NSDecimalNumber alloc] initWithString:weight];//单个重量
                    
                    NSDecimalNumber *zongweightDM=[[NSDecimalNumber alloc]initWithString:@"0"];
                    
                    NSString *weightName;
                    if (unitpric==10) {
                        //元每吨
                        NSDecimalNumber *qian = [[NSDecimalNumber alloc] initWithString:@"1000"];
                        zongweightDM = [quantityDM decimalNumberByMultiplyingBy:weightDM];
                        zongweightDM=[zongweightDM decimalNumberByDividingBy:qian];
                        weightName=[NSString stringWithFormat:@"%@吨",[NSString notRounding:zongweightDM afterPoint:3]];
                    } else if (unitpric==20||unitpric==40) {
                        //支或张
                        zongweightDM = [quantityDM decimalNumberByMultiplyingBy:weightDM];
                        weightName=[NSString stringWithFormat:@"%@千克",[NSString notRounding:zongweightDM afterPoint:3]];
                        
                    } else {
                        //千克
                        zongweightDM = [quantityDM decimalNumberByMultiplyingBy:weightDM];
                        weightName=[NSString stringWithFormat:@"%@千克",[NSString notRounding:zongweightDM afterPoint:3]];
                        
                    }
                    
                    NSString *bigTEXT;
                    if ([gouwuMD.CATEGORY_NAME isEqualToString:@"板"]||[gouwuMD.CATEGORY_NAME isEqualToString:@"板材"]||[gouwuMD.CATEGORY_NAME isEqualToString:@"卷材"]||[gouwuMD.CATEGORY_NAME isEqualToString:@"卷"]||[gouwuMD.CATEGORY_NAME isEqualToString:@"带材"]||[gouwuMD.CATEGORY_NAME isEqualToString:@"带"]) {
                        //            材质 规格(厚度*宽度*长度） 参厚 等级 重量 仓库
                        
                        bigTEXT=[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@",toplable,guige,canhou,dengji,weightName,ck];
                        
                        
                    }else{
                        
                        //       材质表面 规格 品类（如扁钢等） 形状（如方管、圆管等，客户没有上传就不展示）
                        
                        bigTEXT=[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@",toplable,guige,gouwuMD.CATEGORY_NAME,gouwuMD.SHAPE,weightName,ck];
                    }
                    
                    
                    UILabel *contTextlabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, screenWidth-10, 40)];
                    contTextlabel.text=bigTEXT;
                    contTextlabel.font=[UIFont systemFontOfSize:12];
                    contTextlabel.textAlignment=NSTextAlignmentLeft;
                    [contTextlabel setNumberOfLines:0];
                    contTextlabel.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                    [cell addSubview:contTextlabel];
                    
                    
                    //分割线
                    UIView *vwimg=[[UIView alloc]init];
                    vwimg.backgroundColor=[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
                    vwimg.frame=CGRectMake(10, 40-1, screenWidth-1, 1);
                    [cell addSubview:vwimg];
                    
                    
                }
                
            }

        
        
            if (indexPath.row==self.OrderArray.count+1) {
                
                UILabel *liuyan=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 60, 20)];
                liuyan.text=@"买家留言:";
                liuyan.font=[UIFont systemFontOfSize:12];
                liuyan.textAlignment=NSTextAlignmentLeft;
                liuyan.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                [cell addSubview:liuyan];
                
                //留言填写
                liuyanText=[[UITextView alloc]initWithFrame:CGRectMake(70, 5, screenWidth-80, weighttext-20)];
                liuyanText.font=[UIFont systemFontOfSize:12];
                liuyanText.textAlignment=NSTextAlignmentLeft;
                liuyanText.text=toBeString;
                liuyanText.delegate=self;
//                liuyanText.delegate=_tableview;
                liuyanText.textColor=[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0  alpha:1];
                [cell addSubview:liuyanText];
                
                
                //分割线
                vwimgtext=[[UIView alloc]init];
                vwimgtext.backgroundColor=[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
                vwimgtext.frame=CGRectMake(10, weighttext-1, screenWidth-1, 1);
                [cell addSubview:vwimgtext];
                
                
                
            }
            
            
            if (indexPath.row==self.OrderArray.count+2) {
                
                UILabel *youhui=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 60, 20)];
                youhui.text=@"优惠券";
                youhui.font=[UIFont systemFontOfSize:12];
                youhui.textAlignment=NSTextAlignmentLeft;
                youhui.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                [cell addSubview:youhui];
                
                
//                keyongNum=[[UILabel alloc]initWithFrame:CGRectMake(70, 10, 70, 20)];
//                keyongNum.text=wukeyong;
//                keyongNum.font=[UIFont systemFontOfSize:12];
//                keyongNum.textAlignment=NSTextAlignmentLeft;
//                keyongNum.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
//                [cell addSubview:keyongNum];
                
                
                UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(70, 10, 45, 20)];
                imageview.image=[UIImage imageNamed:@"wukeyongimg"];
                imageview.tag=200+indexPath.row;
                [cell addSubview:imageview];
                
                if ([wukeyong isEqualToString:@"无可用"]) {
                    imageview.hidden=NO;
                }else{
                    imageview.hidden=YES;
                }

                
            
                rightlabel1=[[UILabel alloc]initWithFrame:CGRectMake(60, 10, screenWidth-60-30, 20)];
                //        NSString *st=[NSString stringWithFormat:@"共%d件,查看详情",self.OrderArray.count];
                rightlabel1.textAlignment=NSTextAlignmentRight;
                rightlabel1.font=[UIFont systemFontOfSize:12];
                rightlabel1.text=weishiyong;
                rightlabel1.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                [cell addSubview:rightlabel1];
                
                //分割线
                UIView *vwimg=[[UIView alloc]init];
                vwimg.backgroundColor=[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
                vwimg.frame=CGRectMake(10, 40-1, screenWidth-1, 1);
                [cell addSubview:vwimg];
                
                
            }
            if (indexPath.row==self.OrderArray.count+3) {
                
                
                
                cell.backgroundColor=[UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
                
                UIView *fatherView=[[UIView alloc]initWithFrame:CGRectMake(0, 10, screenWidth, 200)];
                fatherView.backgroundColor=[UIColor whiteColor];
                [cell addSubview:fatherView];
                
                
                NSArray *array=@[@"货品总金额",@"出库费",@"短驳费",@"优惠金额"];
                for (int i=0; i<array.count; i++) {
                    
                    youhui=[[UILabel alloc]initWithFrame:CGRectMake(10, 10+(20+10+10)*i, 60, 20)];
                    youhui.text=array[i];
                    youhui.font=[UIFont systemFontOfSize:12];
                    youhui.textAlignment=NSTextAlignmentLeft;
                    youhui.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                    [fatherView addSubview:youhui];
                    
                    
                    youhuiText=[[UILabel alloc]initWithFrame:CGRectMake(80, 10+(20+10+10)*i, screenWidth-80-10, 20)];
                    youhuiText.text=MoneytextArray[i];
                    youhuiText.tag=1000+i;
                    youhuiText.font=[UIFont systemFontOfSize:12];
                    youhuiText.textAlignment=NSTextAlignmentRight;
                    youhuiText.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                    [fatherView addSubview:youhuiText];
                    
                    
                }
                
                //实付款
                shifuText=[[UILabel alloc]initWithFrame:CGRectMake(80, 10+160, screenWidth-80-10, 20)];
                //        shifuText.text=@"实付款：￥150150";
                shifuText.font=[UIFont systemFontOfSize:12];
                shifuText.textAlignment=NSTextAlignmentRight;
                shifuText.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
                [fatherView addSubview:shifuText];
                
            }

            
            
            
            
        
        }
        
        
        
        
        
        
    }
    
    if ([self.isXianhuo isEqualToString:@"现货列表"]) {
        
        
        if (indexPath.row==2) {
            
            liuyanText.text=toBeString;
        }
        if (indexPath.row==3) {
//            keyongNum.text=wukeyong;
            
            UIImageView *im=[cell viewWithTag:200+indexPath.row];
            
            if ([wukeyong isEqualToString:@"无可用"]) {
                im.hidden=NO;
            }else{
                im.hidden=YES;
            }
            
            rightlabel1.text=weishiyong;
        }
        
        
        if (indexPath.row==4) {
            
            //指定位置变色
            NSString *ss=[NSString stringWithFormat:@"实付款:￥%@",[NSString notRounding:shifuMoneyDM afterPoint:2]];
            NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:ss];
            NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:@":"].location+1,noteStr.length-4 );
            [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];
            [shifuText setAttributedText:noteStr] ;
            
            
            for (int i=0; i<4; i++) {
                
                UILabel *find_label = (UILabel *)[self.view viewWithTag:i+1000];
                
                find_label.text=MoneytextArray[i];
            }
            
            
            
        }

        
        
        
    }else{
        
        if (indexPath.row==self.OrderArray.count+1) {
            
            liuyanText.text=toBeString;
        }
        if (indexPath.row==self.OrderArray.count+2) {
//            keyongNum.text=wukeyong;
            UIImageView *im=[cell viewWithTag:200+indexPath.row];
            
            if ([wukeyong isEqualToString:@"无可用"]) {
                im.hidden=NO;
            }else{
                im.hidden=YES;
            }

            
            rightlabel1.text=weishiyong;
        }
        
        
        if (indexPath.row==self.OrderArray.count+3) {
            
            //指定位置变色
            NSString *ss=[NSString stringWithFormat:@"实付款:￥%@",[NSString notRounding:shifuMoneyDM afterPoint:2]];
            NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:ss];
            NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:@":"].location+1,noteStr.length-4 );
            [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];
            [shifuText setAttributedText:noteStr] ;
            
            
            for (int i=0; i<4; i++) {
                
                UILabel *find_label = (UILabel *)[self.view viewWithTag:i+1000];
                
                find_label.text=MoneytextArray[i];
            }
            
            
            
        }

        
    
    
    }
    return cell;
}

//选中单元格时执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    // 取消选中状态
    //    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if ([self.isXianhuo isEqualToString:@"现货列表"]) {
        
        if (indexPath.row==0) {
            //货品清单
            huopinVC *hpVc=[[huopinVC alloc]init];
            hpVc.isXianhuo=self.isXianhuo;
            hpVc.gouwuMDT=self.gouwuMDT;
            hpVc.zongmoney=zongmoney;
            [self.navigationController pushViewController:hpVc animated:YES];
            
        }

        
        if (indexPath.row==3) {
            
            youhuiquanVC *yhqVc=[[youhuiquanVC alloc]init];
            yhqVc.productArryStr=self.gouwuMDT.PRODUCT_ID;
            yhqVc.isgouwuche=@"现货列表";
            yhqVc.QUANTITY=self.gouwuMDT.QUANTITY;
            [self.navigationController pushViewController:yhqVc animated:YES];
            
        }

    
        
    }else{
        
        if (indexPath.row==0) {
            //货品清单
            huopinVC *hpVc=[[huopinVC alloc]init];
            hpVc.OrderArray=self.OrderArray;
            hpVc.zongmoney=zongmoney;
            [self.navigationController pushViewController:hpVc animated:YES];
            
        }

        if (indexPath.row==self.OrderArray.count+2) {
            
        
            NSMutableArray *productIDarray=[[NSMutableArray alloc]init];
            
            for (int i=0; i<self.OrderArray.count; i++) {
                
                gouwucartModel *goucarM=[[gouwucartModel alloc]initWith:self.OrderArray[i]];
                
                [productIDarray addObject:goucarM.PRODUCT_ID];
                
            }
            
           NSString *string = [productIDarray componentsJoinedByString:@","];

            youhuiquanVC *yhqVc=[[youhuiquanVC alloc]init];
            yhqVc.productArryStr=string;
            yhqVc.isgouwuche=@"购物车";
           [self.navigationController pushViewController:yhqVc animated:YES];
            
        }

    
    
    }
    
    
    
    
    
}

#pragma mark----------textView代理方法----------
- (void)textViewDidBeginEditing:(UITextView *)textView {
    if ([textView.text isEqualToString:kOrdeRequirements]) {
        textView.text = @"";
    }
}

-(void)textViewDidChange:(UITextView *)textView
{
    NSString  *nsTextContent = textView.text;
    NSInteger existTextNum = nsTextContent.length;

    if (existTextNum > 50)
    {
        //截取到最大位置的字符

        NSString *s = [nsTextContent substringToIndex:50];
    
        [textView setText:s];
    }
    
}



//当输入类容的时候
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //通过检测UITextView  输入的内容   得到它的内容高度   把内容的高度 设置成 inputView的高度  以及BgView的高度
    //textView.contentSize.height输入内容的高度
    //    self.downView.frame = CGRectMake(0, CGRectGetHeight([UIScreen mainScreen].bounds)-textView.contentSize.height-10-CGRectGetHeight(keyBoardRect), CGRectGetWidth([UIScreen mainScreen].bounds), textView.contentSize.height+10);  //+10     -10 :因为textView的高度上下分别离bgView有5个像素
    //    self.shanghubeizhu.frame = CGRectMake(50, 5, CGRectGetWidth(self.downView.frame)-50*2-50, textView.contentSize.height);
    
    
    if ([text isEqualToString:@"\n"]) {
        
        [textView resignFirstResponder];
        return NO;
    }
    
    
//    if ([text isEqualToString:@"\n"])  //yes  按会车可以改变
//        
//    {
//        
//        return YES;
//                
//    }

    
    if (range.location>=50)
    {
        UIAlertView * alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"您已输入50个字" delegate:nil cancelButtonTitle:@"返回" otherButtonTitles: nil];
        [alert show];
        return NO;
    }
    else
    {
        return YES;
    }

}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    UITableViewCell * cell = (UITableViewCell *)[[textView superview] superview];
    NSIndexPath *indexPath = [self.tableview indexPathForCell:cell];
        //加号按钮
     toBeString=textView.text;
    
     weighttext=[self stringRect:toBeString uiWidth:screenWidth-80 font:[UIFont systemFontOfSize:12]]+20;

    if (weighttext<40) {
        
        weighttext=40;
        
    }
    
    liuyanText.frame=CGRectMake(70, 10, screenWidth-80, weighttext-20);
    vwimgtext.frame=CGRectMake(10, weighttext-1, screenWidth-1, 1);
    [self.tableview reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
    
    
}
////编辑框事件
//- (void)textViewDidBeginEditing:(UITextView *)textView;
//{
//    UITableViewCell * cell = (UITableViewCell *)[textView superview];
//    NSIndexPath *indexPath = [self.tableview indexPathForCell:cell];
//    //加号按钮
//    NSLog(@"%ld====%ld",(long)indexPath.section,(long)indexPath.row);
//
//    
//    CGRect rectInTableView = [_tableview rectForRowAtIndexPath:indexPath];
//    
//    
//    [_tableview setContentOffset: CGPointMake( 0, rectInTableView.origin.y-100 ) animated: NO];
//    
////        [UIView animateWithDuration:0.25 animations:^{
////            self.view.frame=CGRectMake(0, -200, self.view.frame.size.width, self.view.frame.size.height-140-50);
////        }];
////    (0,145, self.view.frame.size.width, self.view.frame.size.height-140-50)];
//}



#pragma mark--------------其他方法
//计算文字高度
-(NSInteger)stringRect:(NSString*)str uiWidth:(NSInteger)widt font:(UIFont*)lfont
{
   
//    return [self stringRect:_Datalist.Remark uiWidth:140 font:[UIFont systemFontOfSize:15]]+18;
    
    //MAXFLOAT
    CGRect rect = [str boundingRectWithSize:CGSizeMake(widt, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:[[NSDictionary alloc] initWithObjectsAndKeys:lfont,NSFontAttributeName, nil] context:nil];
    return rect.size.height;
}

@end
